{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d7392fcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b4ee2f17c7754436b4265528a2403b23",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7f72d9258d4a4dcc997c8ca1ba802849",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Button(button_style='info', description='Change Color', style=ButtonStyle(), tooltip='click to select a random…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "21f8b6cd01994d15a41d621983375390",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "FloatSlider(value=0.0, description='x-position', max=0.2, min=-0.2, step=0.02)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import display, clear_output\n",
    "import ipywidgets as widgets\n",
    "from vedo import *\n",
    "\n",
    "############################## button\n",
    "button = widgets.Button(\n",
    "    description='Change Color',\n",
    "    button_style='info',\n",
    "    tooltip='click to select a random color',\n",
    ")\n",
    "\n",
    "def on_button_click(b):\n",
    "    with out:\n",
    "        rgb = (np.random.rand(3)*255).astype(int)\n",
    "        mesh.color(rgb)\n",
    "        vtxt.text(f\"RGB: {rgb} \\n({get_color_name(rgb)})\")\n",
    "        clear_output(wait=True)\n",
    "        img = plt.show()\n",
    "        display(img)\n",
    "button.on_click(on_button_click)\n",
    "\n",
    "############################## slider\n",
    "slider = widgets.FloatSlider(\n",
    "    description='x-position', \n",
    "    min=-0.2, value=0, max=0.2, \n",
    "    step=0.02,\n",
    "    continuous_update=True,\n",
    ")\n",
    "\n",
    "def on_slider(change):\n",
    "    with out:\n",
    "        value = change['new']\n",
    "        mesh.x(slider.value)\n",
    "        clear_output(wait=True)\n",
    "        img = plt.show(resetcam=True)\n",
    "        display(img)\n",
    "slider.observe(on_slider, names='value')\n",
    "\n",
    "############################################## vedo\n",
    "# settings.default_backend = \"2d\"  # this example only works with \"2d\"\n",
    "settings.backend_autoclose = False  # do not close Plotter after show()\n",
    "\n",
    "plt = Plotter(size=[600,500], axes=1, bg2=\"lightblue\")\n",
    "mesh = Mesh(dataurl+\"bunny.obj\").color(\"white\")\n",
    "vtxt = Text2D(font=\"Cartoons123\")\n",
    "plt += [mesh, vtxt]\n",
    "\n",
    "out = widgets.Output()\n",
    "display(out, button, slider)\n",
    "with out:\n",
    "    img = plt.show()\n",
    "    display(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d3d61d2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f1ac44ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr>\n",
       "<td>\n",
       "<img src=''></img>\n",
       "</td>\n",
       "<td style='text-align: center; vertical-align: center;'><br/>\n",
       "<b> Mesh: &nbsp&nbsp</b><b><a href=\"https://vedo.embl.es/docs/vedo/mesh.html#Mesh\" target=\"_blank\">vedo.mesh.Mesh</a></b><br/><code><i>(...mbl.es/examples/data/bunny.obj)</i></code>\n",
       "<table>\n",
       "<tr><td><b> bounds </b> <br/> (x/y/z) </td><td>-0.09438 ... 0.06078<br/>0.03331 ... 0.1870<br/>-0.06168 ... 0.05871</td></tr>\n",
       "<tr><td><b> center of mass </b></td><td>(-0.0280, 0.0942, 9.05e-3)</td></tr>\n",
       "<tr><td><b> average size </b></td><td>0.063</td></tr>\n",
       "<tr><td><b> nr. points&nbsp/&nbspfaces </b></td><td>2503&nbsp/&nbsp4968</td></tr>\n",
       "\n",
       "\n",
       "</table>\n",
       "</table>"
      ],
      "text/plain": [
       "<vedo.mesh.Mesh at 0x7f35ccc8dd50>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mesh\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10545467-9489-42e6-a4ea-a331242ab9ae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
